package com.example.demo.model;

import jakarta.persistence.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;

import org.springframework.security.core.GrantedAuthority;

import javax.swing.text.StyledEditorKit;

@Slf4j
@Entity
@Table(name = "user")
public class User implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)  // 自增主键
    private Long id;

    @Column(nullable = false, unique = true)  // 非空且唯一
    private String username;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    private String email;

    @Column(nullable = false)
    private String role = "USER";  // 添加角色字段，默认值为 USER

    @Column(name = "is_vip", nullable = false, columnDefinition = "TINYINT(1) DEFAULT 0")
    private boolean isVip = false;

    @Column(name = "vip_expiry")
    private LocalDateTime vipExpiry;

    @Column(name = "is_banned", nullable = false, columnDefinition = "TINYINT(1) default 0")
    private boolean banned = false;

    @Column(name = "ban_reason")
    private String banReason;

    @Column(name = "ban_time")
    private LocalDateTime banTime;


    @Column(name = "ban_expiry")
    private LocalDateTime banExpiry; // 新增封禁到期时间字段

    @Column(name = "upload") // 上传量
    private String upload = "0";

    @Column(name = "download") // 下载量
    private String download = "0";

    @Column(name = "share_rating") //分享率
    private double shareRating = 0.0;

    public static final String ROLE_ADMIN = "ADMIN";
    public static final String ROLE_USER = "USER";

    public User(){}

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        // 检查封禁状态和到期时间
        if (banned && banExpiry != null && banExpiry.isBefore(LocalDateTime.now())) {
            setBanned(false);
            setBanReason(null);
            setBanTime(null);
            setBanExpiry(null);
        }
        log.info("检查用户封禁状态: banned={}", banned);
        return !banned;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.singletonList(
                new SimpleGrantedAuthority("ROLE_" + this.role.toUpperCase())
        );
    }

    // 添加角色字段的 getter 和 setter
    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public Long getId() {
        return id;
    }

    public boolean isAdmin() {
        return "ADMIN".equals(this.role);
    }

    public boolean getBanned(){return banned;
    }

    public void setBanned(boolean banned) {this.banned = banned;
    }

    public String getBanReason(){
        return banReason;
    }

    public void setBanReason(String banReason){
        this.banReason = banReason;
    }

    public LocalDateTime getBanTime(){
        return banTime;
    }

    public void setBanTime(LocalDateTime banTime){
        this.banTime = banTime;
    }

    public boolean isBanned() {
        // 动态检查封禁状态
        if (banned && banExpiry != null && banExpiry.isBefore(LocalDateTime.now())) {
            setBanned(false);
            setBanReason(null);
            setBanTime(null);
            setBanExpiry(null);
        }
        return banned;
    }

    public boolean getIsVip(){
        return isVip;
    }
    public void setIsVip(boolean isVip){
        this.isVip = isVip;
    }

    public LocalDateTime getVipExpiry() {
        return vipExpiry;
    }
    public void setVipExpiry(LocalDateTime vipExpiry){
        this.vipExpiry = vipExpiry;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDateTime getBanExpiry() {
        return banExpiry;
    }

    public void setBanExpiry(LocalDateTime banExpiry) {
        this.banExpiry = banExpiry;
    }

    public String getUpload() {
        return upload;
    }

    public void setUpload(String upload) {
        this.upload = upload;
    }

    public String getDownload() {
        return download;
    }

    public void setDownload(String download) {
        this.download = download;
    }

    public double getShareRating() {
        return shareRating;
    }

    public void setShareRating(double shareRating) {
        this.shareRating = shareRating;
    }
}
